package com.librariy.view;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import com.bcia.assetmange.R;
import com.librariy.util.SystemDisplay;
public class TitleBar extends FrameLayout implements View.OnClickListener {
    public SystemDisplay sd=SystemDisplay.getInstance(null);
    public static final int EVENT_BACK=0x01;
    public static final int EVENT_MENU=0x02;
    private ImageView left;
    private TextView title;
    private LinearLayout right;
    private Bundle style=new Bundle();
    public TitleBar(Context context) {
        super(context);
        this.initialize(context,null);
    }
    public TitleBar(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        this.initialize(context,attrs);
    }
    public TitleBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.initialize(context,attrs);
    }
    private void initialize(Context mContext,AttributeSet attrs) {
        //View创建
        this.title=new TextView(mContext);
        this.title.setGravity(Gravity.CENTER);
        super.addView(title,new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, (int)(sd.density*50), Gravity.CENTER));
        this.left=new ImageView(mContext);
        this.left.setTag(EVENT_BACK);
        this.left.setOnClickListener(this);
        this.left.setPadding((int)(sd.density*10),0,(int)(sd.density*10),0);
        super.addView(left,new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, (int)(sd.density*50), Gravity.START|Gravity.CENTER_VERTICAL));
        this.right=new LinearLayout(mContext);
        this.right.setOrientation(LinearLayout.HORIZONTAL);
        this.right.setGravity(Gravity.CENTER);
        this.right.setPadding((int)(sd.density*5),0,(int)(sd.density*5),0);
        super.addView(right,new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, (int)(sd.density*50), Gravity.END|Gravity.CENTER_VERTICAL));
        //设置标题, 个元素的可见性等
        this.setTitle(attrs==null?null:attrs.getAttributeValue(Namespace.PACTERA,"title_text"));
        this.setTitleVisibility(attrs==null?null:attrs.getAttributeValue(Namespace.PACTERA,"title_visible"));
        int iconRes=(attrs==null?0:attrs.getAttributeResourceValue(Namespace.PACTERA,"title_icon",0));
        int titleStyle=(attrs==null)?0:attrs.getAttributeIntValue(Namespace.PACTERA,"title_style",0);
        int textAppearance;
        if(titleStyle==1){
            iconRes=(iconRes!=0)?iconRes:R.drawable.ic_sys_back_black;
            textAppearance=android.R.style.TextAppearance;
        }else{
            iconRes=(iconRes!=0)?iconRes:R.drawable.ic_sys_back_white;
            textAppearance=android.R.style.TextAppearance_Inverse;
        }
        this.left.setImageResource(iconRes);
        TypedArray mTypedArray=mContext.obtainStyledAttributes(attrs,new int[]{android.R.attr.textSize,android.R.attr.textColor},0,textAppearance);
        this.style.putFloat("TextSize",mTypedArray.getDimension(0,16*sd.density));
        this.style.putInt("TextColor",mTypedArray.getColor(1,0xFF000000));
        mTypedArray.recycle();
        this.title.setTextColor(style.getInt("TextColor",0));
        this.title.setTextSize(TypedValue.COMPLEX_UNIT_PX,style.getFloat("TextSize",0));
    }
    public void setTitle(CharSequence title){
        this.title.setText(TextUtils.isEmpty(title)?"":title);
    }
    public void addMenu(boolean reset,CharSequence menuName,final int id,final MenuEvent listener){
        this.addMenu(reset,menuName,0,0,0,0,id,listener);
    }
    public void addMenu(boolean reset,CharSequence menuName,int start, int top,int end, int bottom,final int id,final MenuEvent listener){
        if(reset){
            right.removeAllViews();
        }
        TextView tv=new TextView(getContext());
        tv.setPadding((int)(sd.density*5),0,(int)(sd.density*5),0);
        tv.setTextColor(style.getInt("TextColor",0));
        tv.setTextSize(TypedValue.COMPLEX_UNIT_PX,style.getFloat("TextSize",0));
        tv.setText(menuName);
        tv.setCompoundDrawablesWithIntrinsicBounds(start,top,end,bottom);
        tv.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                listener.onMenuClick(id);
            }
        });
        this.right.addView(tv,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
        this.right.setVisibility(right.getChildCount()>0?View.VISIBLE:View.GONE);
    }
    public void setTitleVisibility(String titleVisible){
        titleVisible=(titleVisible==null)?"LI|TT":titleVisible;
        this.left.setVisibility(titleVisible.contains("LI")?View.VISIBLE:View.GONE);
        this.title.setVisibility(titleVisible.contains("TT")?View.VISIBLE:View.GONE);
        this.right.setVisibility((titleVisible.contains("RT")||titleVisible.contains("RI"))?View.VISIBLE:View.GONE);
    }
    @Override
    public void onClick(View v) {
        Integer event=(Integer) v.getTag();
        Log.e("TitleBar","EVENT="+event);
        if(event==EVENT_BACK) {
            super.getRootView().dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
            super.getRootView().dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));
        }
    }
    public interface MenuEvent{
        void onMenuClick(int id);
    }
}